package com.epin.base.plugins;

import java.math.BigDecimal;

import com.feilong.core.Validator;
import com.epin.base.enums.commons.CapitalAmountEnum;
import com.epin.base.enums.commons.ChineseCapitalEnum;

public class CapitalAmount {
	
	private static final int size = 9;
	
	/**
	 * 分
	 */
	private String fen;
	
	/**
	 * 角
	 */
	private String jiao;
	
	/**
	 * 元
	 */
	private String yuan;
	
	/**
	 * 十
	 */
	private String ten;
	
	/**
	 * 百
	 */
	private String hundred;
	
	/**
	 * 千
	 */
	private String thousand;
	
	/**
	 * 万
	 */
	private String tenThousand;
	
	/**
	 * 十万
	 */
	private String hundredThousand;
	
	/**
	 * 百万
	 */
	private String thousandThousand;
	
	
	public String getFen() {
		return fen;
	}


	public void setFen(String fen) {
		this.fen = fen;
	}


	public String getJiao() {
		return jiao;
	}


	public void setJiao(String jiao) {
		this.jiao = jiao;
	}


	public String getYuan() {
		return yuan;
	}


	public void setYuan(String yuan) {
		this.yuan = yuan;
	}


	public String getTen() {
		return ten;
	}


	public void setTen(String ten) {
		this.ten = ten;
	}


	public String getHundred() {
		return hundred;
	}


	public void setHundred(String hundred) {
		this.hundred = hundred;
	}


	public String getThousand() {
		return thousand;
	}


	public void setThousand(String thousand) {
		this.thousand = thousand;
	}


	public String getTenThousand() {
		return tenThousand;
	}


	public void setTenThousand(String tenThousand) {
		this.tenThousand = tenThousand;
	}


	public String getHundredThousand() {
		return hundredThousand;
	}


	public void setHundredThousand(String hundredThousand) {
		this.hundredThousand = hundredThousand;
	}


	public String getThousandThousand() {
		return thousandThousand;
	}


	public void setThousandThousand(String thousandThousand) {
		this.thousandThousand = thousandThousand;
	}
	
	
	public CapitalAmount() {
		super();
		this.fen = "";
		this.jiao = "";
		this.yuan = "";
		this.ten = "";
		this.hundred = "";
		this.thousand = "";
		this.tenThousand = "";
		this.hundredThousand = "";
		this.thousandThousand = "";
	}
	
	public CapitalAmount(int value) {
		super();
		value = (value >= 10 || value < 0) ? 0 : value; 
		this.fen = String.valueOf(value);
		this.jiao = String.valueOf(value);
		this.yuan = String.valueOf(value);
		this.ten = String.valueOf(value);
		this.hundred = String.valueOf(value);
		this.thousand = String.valueOf(value);
		this.tenThousand = String.valueOf(value);
		this.hundredThousand = String.valueOf(value);
		this.thousandThousand = String.valueOf(value);
	}
	
	public CapitalAmount(ChineseCapitalEnum value) {
		super();
		this.fen = value.msg();
		this.jiao = value.msg();
		this.yuan = value.msg();
		this.ten = value.msg();
		this.hundred = value.msg();
		this.thousand = value.msg();
		this.tenThousand = value.msg();
		this.hundredThousand = value.msg();
		this.thousandThousand = value.msg();
	}
	

	@Override
	public String toString() {
		return super.toString();
	}
	
	
	/**
	 * <p>统计类转大写类</p>
	 * 
	 * @param amount
	 * 
	 * @return
	 * 
	 * @author fujiajun
	 * @since 2017-10-11
	 */
	public static CapitalAmount createCapitalAmount(BigDecimal amount){
		
		CapitalAmount capitalAmount = new CapitalAmount(ChineseCapitalEnum.ZERO);
		
		if (Validator.isNotNullOrEmpty(amount)) {
			long number = amount.setScale(2, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100)).longValue();
			
			for (int i = 0; number != 0; i++, number /= 10) {
				
				String capital = ChineseCapitalEnum.toString((int) (number % 10));
				
				switch (i) {
				case 0:
					capitalAmount.setFen(capital);
					break;
				case 1:
					capitalAmount.setJiao(capital);
					break;
				case 2:
					capitalAmount.setYuan(capital);
					break;
				case 3:
					capitalAmount.setTen(capital);
					break;
				case 4:
					capitalAmount.setHundred(capital);
					break;
				case 5:
					capitalAmount.setThousand(capital);
					break;
				case 6:
					capitalAmount.setTenThousand(capital);
					break;
				case 7:
					capitalAmount.setHundredThousand(capital);
					break;
				case 8:
					capitalAmount.setThousandThousand(capital);
					break;
				default:
					break;
				}
			}
		}
		
		return capitalAmount;
	}
	
	
	/**
	 * <p>大写类转统计类</p>
	 * 
	 * @param amount
	 * 
	 * @return
	 * 
	 * @author fujiajun
	 * @since 2017-10-11
	 */
	public static BigDecimal toBigDecimal(CapitalAmount amount) {
		
		BigDecimal result = new BigDecimal(0).setScale(2, BigDecimal.ROUND_HALF_UP);
		
		for(int i = 0; i < size; i++){
			
			BigDecimal value = null;
			BigDecimal digit = null;
			
			switch (i) {
			case 0:
				value = new BigDecimal(ChineseCapitalEnum.toValue(amount.getFen()));
				digit = new BigDecimal(CapitalAmountEnum.FEN.val());
				break;
			case 1:
				value = new BigDecimal(ChineseCapitalEnum.toValue(amount.getJiao()));
				digit = new BigDecimal(CapitalAmountEnum.JIAO.val());
				break;
			case 2:
				value = new BigDecimal(ChineseCapitalEnum.toValue(amount.getYuan()));
				digit = new BigDecimal(CapitalAmountEnum.YUAN.val());
				break;
			case 3:
				value = new BigDecimal(ChineseCapitalEnum.toValue(amount.getTen()));
				digit = new BigDecimal(CapitalAmountEnum.TEN.val());
				break;
			case 4:
				value = new BigDecimal(ChineseCapitalEnum.toValue(amount.getHundred()));
				digit = new BigDecimal(CapitalAmountEnum.HUNDRED.val());
				break;
			case 5:
				value = new BigDecimal(ChineseCapitalEnum.toValue(amount.getThousand()));
				digit = new BigDecimal(CapitalAmountEnum.THOUSAND.val());
				break;
			case 6:
				value = new BigDecimal(ChineseCapitalEnum.toValue(amount.getTenThousand()));
				digit = new BigDecimal(CapitalAmountEnum.TEN_THOUSAND.val());
				break;
			case 7:
				value = new BigDecimal(ChineseCapitalEnum.toValue(amount.getHundredThousand()));
				digit = new BigDecimal(CapitalAmountEnum.HUNDRED_THOUSAND.val());
				break;
			case 8:
				value = new BigDecimal(ChineseCapitalEnum.toValue(amount.getThousandThousand()));
				digit = new BigDecimal(CapitalAmountEnum.THOUSAND_THOUSAND.val());
				break;
			default:
				value = new BigDecimal(0);
				digit = new BigDecimal(0);
				break;
			}

			value = value.setScale(2, BigDecimal.ROUND_HALF_UP);
			digit = digit.setScale(2, BigDecimal.ROUND_HALF_UP);
			
			value = value.multiply(digit).setScale(2, BigDecimal.ROUND_HALF_UP);
			
			result = result.add(value);
		}
		
		return result;
	}
}
